package com.yuanfengerp.customersettlemer.service.impl;


import com.yuanfengerp.customerexpenses.model.CustomerExpenseItemsInfo;
import com.yuanfengerp.customersettlemer.dao.ICustomerSettlementMapper;
import com.yuanfengerp.customersettlemer.model.CustomerSettlementInfo;
import com.yuanfengerp.customersettlemer.model.CustomerSettlementItemInfo;
import com.yuanfengerp.customersettlemer.service.ICustomerSettlementService;
import com.yuanfengerp.publicUtil.Result;
import com.yuanfengerp.publicUtil.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CustomerSettlementServiceImpl implements ICustomerSettlementService {
    @Autowired
    private ICustomerSettlementMapper icsm;

    @Override
    public Result<String> batchdelete(ArrayList<String> customer_settlement_IDs) {
        return null;
    }

    @Override
    public Result<String> batchreview(ArrayList<String> customer_settlement_IDs) {
        return null;
    }

    @Override
    public Result<String> deleteByPrimaryKey(String customer_settlement_ID) {
        int i = icsm.deleteByPrimaryKey(customer_settlement_ID);
        return i > 0 ? Result.success("删除成功") : Result.failure(500, "删除失败");
    }

    @Override
    public Result<String> insertCustomerSettlement(CustomerSettlementInfo customerSettlementInfo) {
        Double value = 0.0;
        customerSettlementInfo.setCustomer_settlement_operate_time(new Timestamp(System.currentTimeMillis()));
        customerSettlementInfo.setCustomer_settlement_state(0);
        customerSettlementInfo.setCustomer_settlement_review_time(new Timestamp(System.currentTimeMillis()));
        SnowflakeIdWorker worker = new SnowflakeIdWorker(1);
        customerSettlementInfo.setCustomer_settlement_ID(worker.nextId());

        int i = icsm.insertCustomerSettlement(customerSettlementInfo);
        if (i > 0) {
            // 如果插入成功，继续处理连表新增的费用项信息插入
            List<CustomerSettlementItemInfo> customerSettlementItemInfoList = customerSettlementInfo.getCustomerSettlementItemInfoList();
            if (customerSettlementItemInfoList != null && !customerSettlementItemInfoList.isEmpty()) {
                for (CustomerSettlementItemInfo customerSettlementItemInfo : customerSettlementItemInfoList) {
                    value = customerSettlementItemInfo.getCustomer_expenses_total_value();
                    customerSettlementItemInfo.setCustomer_expenses_ID(customerSettlementInfo.getCustomer_settlement_ID());
                    int j = icsm.insertCustomerSettlementItemInfo(customerSettlementItemInfo);
                    if (j <= 0) {
                        // 如果插入连接表失败，需要进行相应处理，这里简单返回失败结果
                        return Result.failure(500, "添加费用项到连接表失败");
                    }
                }
            }

            customerSettlementInfo.setCustomer_settlement_payable_value(value);
            int i1 = icsm.insertCustomerSettlement(customerSettlementInfo);
            if (i1 <= 0)
                return Result.failure(500, "更新费用信息失败");
            // 如果所有操作都成功，返回成功结果
            return Result.success("添加成功");
        } else {
            // 如果插入失败，返回失败结果并附带错误信息
            return Result.failure(500, "添加失败，数据库插入操作未成功");
        }
    }

    @Override
    public Result<String> updateCustomerSettlement(CustomerSettlementInfo customerSettlementInfo) {
        customerSettlementInfo.setCustomer_settlement_review_time(new Timestamp(System.currentTimeMillis()));
        int i = icsm.updateCustomerSettlement(customerSettlementInfo);
        return null;
    }

    @Override
    public Result<List<CustomerSettlementInfo>> selectAllCustomerSettlement() {
        List<CustomerSettlementInfo> customerSettlementInfo = icsm.selectAllCustomerSettlement();
        if (customerSettlementInfo.isEmpty())
            return Result.failure(400, "没有数据");
        return Result.success(customerSettlementInfo);
    }

    @Override
    public Result<List<CustomerSettlementInfo>> selectCustomerSettlement(Map<String, Object> params) {
        System.out.println(params.toString());
//        params.put("pageSize", ((int) params.get("pageSize") - 1) * (int) params.get("pageNum"));
        params.put("pageSize", ((int) params.get("pageSize") - 1) * (int) params.get("pageNum"));
        List<CustomerSettlementInfo> list = icsm.selectCustomerSettlement(params);
        if (list.isEmpty())
            return Result.failure(400, "没有数据");
        return Result.success(list);
    }

    @Override
    public Result<List<CustomerSettlementInfo>> selectCustomerSettlementItemInfo(String customer_settlement_ID) {
        List<CustomerSettlementInfo> customerSettlementInfo = icsm.selectCustomerSettlementItemInfo(customer_settlement_ID);
        if (customerSettlementInfo == null)
            return Result.failure(400, "没有数据");
        return Result.success(customerSettlementInfo);
    }


}
